const dataBase = require('../../mysql')

const sql = require('../../mysql/sql')
const verify = require('../../auth/verify')
const verifyRequestMethod = require('../../tool/verifyRequestMethod')

/**
 * @description 文章收藏
 */
module.exports = function () {
    const that = this
    /**
     * @description 获取query
     * @type {{}|*}
     */
    const query = this.getQueryData(this.request.url)
    const authorization = this.request.headers.authorization
    /**
     * @description 请求验证
     */
    verifyRequestMethod(this.request, 'GET').then(function () {
        /**
         * @description 授权验证
         */
        if (that.isDef(authorization)) {
            if (!isNaN(parseInt(query.articleId))) {
                return verify(authorization)
            } else {
                return Promise.reject(new Error('无效的文章Id！'))
            }
        } else {
            return Promise.reject(new Error('缺少Authorization！'))
        }
    }).then(function (data) {
        return dataBase.connect(sql.SEL_SELECT_USER_ONE(data.username))
    }).then(function (data) {
        /**
         * @description 当前用户
         */
        const currentUser = data[0]
        /**
         * @description 获取当前用户的收藏数据
         */
        const collections = currentUser.collections
        let setCollections = []
        /**
         * @description 非法字符验证
         */
        /**
         * @description 验证是否是正确的文章ID
         */
        dataBase.connect(sql.SQL_SELECT_ARTICLE_ONE(query.articleId, true)).then(function (result) {
            if (that.isDef(result[0])) {
                /**
                 * @description 收藏为空
                 */
                const currentArticle = result[0]
                let collectionTotal = 0
                /**
                 * @description 消息
                 * @type {string}
                 */
                let messageTip = ''
                /**
                 * @description 统计收藏次数
                 */
                if (currentArticle.collectionTotal !== null) {
                    collectionTotal = parseInt(currentArticle.collectionTotal)
                }
                if (collections === '') {
                    setCollections.push(parseInt(query.articleId))
                    messageTip = '文章收藏成功！'
                    collectionTotal++
                    /**
                     * @description 已收藏
                     */
                } else if (Array.isArray(JSON.parse(collections))) {
                    const currentCollect = JSON.parse(collections)
                    if (!currentCollect.includes(parseInt(query.articleId))) {
                        setCollections = setCollections.concat(currentCollect, parseInt(query.articleId))
                        messageTip = '文章收藏成功！'
                        collectionTotal++
                    } else {
                        setCollections = currentCollect.filter(function (item) {
                            return item !== parseInt(query.articleId)
                        })
                        messageTip = '取消收藏成功！'
                        collectionTotal--
                    }
                }
                /**
                 * @description 不能出现收藏位-1
                 */
                collectionTotal = collectionTotal <= 0 ? null : collectionTotal
                /**
                 * @description 存储操作
                 */
                dataBase.connect(sql.SQL_UPDATE_USER_COLLECT(currentUser.username, setCollections.toString())).then(function (result) {
                    return dataBase.connect(sql.SQL_UPDATE_ARTICLE_COLLECT(query.articleId, collectionTotal))
                }).then(function (result) {
                    that.response.outPrintData({}, messageTip)
                })
            } else {
                that.response.outPrintError('无效的文章Id！')
            }
        })
    }).catch(function (e) {
        that.response.outPrintError((e && e.message) || '未知错误！')
    })
}